package GUI; //done

import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Client.ConnectionToServer;
import Entities.Customer;

import java.awt.Font;

/**
 * This Class controls Customer Search Window.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamel
 */
public class CustomerSearchWindow extends JFrame{
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;

	/** The Customer ID ComboBox. */
	private JComboBox cIDComboBox = null;
	
	/** The Customer ComboBoxc ID Label. */
	private JLabel cIDlLabel = null;
	
	/** The search Button. */
	private JButton searchButton = null;
	
	/** The Customer jContent Panel. */
	private JPanel jContentPane = null;
	
	/** The customer Window. */
	private CustomerWindow customerWindow = null;
	
	/** The add customer interest Window. */
	private AddCustomerInterestWindow addCustomerInterestWindow = null;
	
	/** The customer. */
	private Customer customer = null;
	
	/** The Customer ID. */
	private long CustID = 0;
	
	/** The initialize Window. */
	private String initialitoWindow;
	
	/** The add Customer Interest Frame. */
	private JFrame addCustomerInterestFrame;
	
	/** The Background Label. */
	private JLabel BackgroundLabel;	
	
	/**
	 * Default constructor
	 */
	public CustomerSearchWindow(String initialitoWindow) {
		super();
		this.initialitoWindow = initialitoWindow;
		initialize();		
	}
	
	/**
	 * This function initializes CustomerSearchWindow
	 */
	private void initialize() {
		customer = new Customer(0, "", 0, 0,"");
		this.setContentPane(getJContentPane());
		this.setTitle("Customer SEARCH");
	}
	
	/**
	 * This function initializes JContentPane
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setBackground(SystemColor.activeCaption);
			jContentPane.setLayout(null);
			
			cIDlLabel = new JLabel();
			cIDlLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
			cIDlLabel.setBounds(new Rectangle(42, 78, 136, 22));
			cIDlLabel.setText("Select Customer ID:");
			jContentPane.setLayout(null);
			jContentPane.add(cIDlLabel, null);
			jContentPane.add(getCIDComboBox(), null);
			jContentPane.add(getSearchButton(),null);
			customerWindow = new CustomerWindow();
			customerWindow.getCancelButton().addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setSize(300, 150);
					setContentPane(getJContentPane());								
				}
			});			
			
			BackgroundLabel = new JLabel("");
			BackgroundLabel.setIcon(new ImageIcon(CustomerSearchWindow.class.getResource("/Pictures/bg.png")));
			BackgroundLabel.setBounds(0, 0, 400, 280);
			jContentPane.add(BackgroundLabel);
		}
		return jContentPane;		
	}
	
	/**
	 * This function initializes PIDComboBox	
	 */
	private JComboBox getCIDComboBox(){
		if(cIDComboBox == null){	
			cIDComboBox = new JComboBox();
			cIDComboBox.setBounds(new Rectangle(210, 78, 136, 25));
			long[] customerid = null;
			try{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				customerid = (long[]) connectiontoserver.loadAllCustomers();
				if (customerid == null){
					JOptionPane.showMessageDialog(null, "Customers table is empty!");
					cIDComboBox.addItem(" ");
				}	
				else
				{
					int len = customerid.length;
					if (len > 0)
						CustID = customerid[0];
					if (len == 0)
						cIDComboBox.addItem(":");	
					else
						cIDComboBox.addItem(" ");
					for(int i=0; i < len; i++){
						cIDComboBox.addItem(customerid[i]);
					}	
				}
			}
			catch(Exception e){
				cIDComboBox.addItem(" ");
			}
			cIDComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					CustID = (Long)cIDComboBox.getSelectedItem();	
				}	
			});	
		}	
		return cIDComboBox;
	}

	
	/**
	 * This function initializes searchButton	
	 */
	private JButton getSearchButton() {
		if (searchButton == null) {
			searchButton = new JButton();
			searchButton.setBounds(new Rectangle(135, 147, 119, 34));
			searchButton.setText("Search");
			searchButton.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e){
					switch(initialitoWindow){
					case "UpdateCustomer":
						initializeUpdateCustomerWindow();
						break;
						
					case "AddCustomerInterests":
						initializeAddCustomerInterestWindow();
						break;
					}
				}	
			});
		}
		return searchButton;
	}
	
	/**
	 * This function initializes UpdateCustomerWindow
	 */
	private void initializeUpdateCustomerWindow(){		
		try {
			if (CustID > 0){
				if (customer.loadCustomer(CustID)){
					customerWindow.loadCustomer(customer);
					customerWindow.addUpdateButton();
					this.setSize(350, 300);
					this.setTitle("CUSTOMER UPDATE");
					
					JLabel BackgroundLabel = new JLabel("");
					BackgroundLabel.setIcon(new ImageIcon(CustomerWindow.class.getResource("/Pictures/bg.png")));
					BackgroundLabel.setBounds(0, 0, 350, 300);
					customerWindow.add(BackgroundLabel);
					setContentPane(customerWindow);
				}	
				else
					JOptionPane.showMessageDialog(null, "CUSTOMER NOT EXIST IN DATA BASE");
			}
			else
				JOptionPane.showMessageDialog(null, "PLZ select a client");
		} 
		catch (Exception excp){
			JOptionPane.showMessageDialog(null, "ILEGAL INPUT");	
		}											
	}
	
	/**
	 * This function initializes AddCustomerInterestWindow
	 */
	public void initializeAddCustomerInterestWindow(){
		try {
			if (CustID > 0){
				if (customer.loadCustomer(CustID)){
					addCustomerInterestFrame = new JFrame();
					addCustomerInterestWindow = new AddCustomerInterestWindow();
					addCustomerInterestWindow.loadCustomer(customer);
					addCustomerInterestFrame.setBounds(500,50,460, 600);
					addCustomerInterestFrame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
					addCustomerInterestFrame.setContentPane(addCustomerInterestWindow);
					addCustomerInterestFrame.setTitle("Add Customer Interests");
					addCustomerInterestFrame.setVisible(true);
				}	
				else
					JOptionPane.showMessageDialog(null, "Customer does not exists in DB!");
			}
			else
				JOptionPane.showMessageDialog(null, "Please choose Customer");
		} 
		catch (Exception excp){
			JOptionPane.showMessageDialog(null, "Illegal input!");	
		}			
	}
}
